package TenoriOn
{
	import TenoriOn.UI.Buttons.icons.*;
	import TenoriOn.UI.TextDisplay;
	import TenoriOn.managers.*;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class TenoriOn extends Sprite
	{
		
		public function TenoriOn()
		{	
			var textDisplay:TextDisplay=new TextDisplay();
			textDisplay.x=720;
			textDisplay.y=530;
			addChild(textDisplay);
			
			var grid:Grid=new Grid();
			addChild(grid);
			grid.textDisplay=textDisplay;
			
			sequencerManager=new SequencerManager();
			volumeManager=new VolumeManager();
			octaveManager=new OctaveManager();
			toneManager=new ToneManager();
			gateManager=new GateManager(sequencerManager);
			soundManager=new SoundManager(sequencerManager,octaveManager,volumeManager,toneManager,gateManager);
			
			var sequencerBtn:SequencerButton=new SequencerButton();
			sequencerBtn.x=660;
			sequencerBtn.y=20;
			sequencerBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{sequencerManager.takeControl(grid)});
			addChild(sequencerBtn);
			
			var playBtn:PlayButton=new PlayButton();
			playBtn.x=730;
			playBtn.y=30;
			playBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{soundManager.play();grid.manager.updateGrid();});
			addChild(playBtn);
			
			var stopBtn:StopButton=new StopButton();
			stopBtn.x=730;
			stopBtn.y=30;
			stopBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{soundManager.stop();grid.manager.updateGrid();});
			addChild(stopBtn);
			stopBtn.visible=false;
			soundManager.addEventListener(SoundManager.PLAYING,function(event:Event):void{stopBtn.visible=true; playBtn.visible=false;});
			soundManager.addEventListener(SoundManager.STOPPED,function(event:Event):void{stopBtn.visible=false; playBtn.visible=true;});
			
			var clearBtn:ClearButton=new ClearButton();
			clearBtn.x=780;
			clearBtn.y=30;
			clearBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{sequencerManager.clearGrid();});
			addChild(clearBtn);
			
			var volumeBtn:VolumeButton=new VolumeButton();
			volumeBtn.x=660;
			volumeBtn.y=100;
			volumeBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{volumeManager.takeControl(grid)});
			addChild(volumeBtn);
			
			var octaveBtn:OctaveButton=new OctaveButton();
			octaveBtn.x=660;
			octaveBtn.y=180;
			octaveBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{octaveManager.takeControl(grid)});
			addChild(octaveBtn);
			
			var toneBtn:ToneButton=new ToneButton();
			toneBtn.x=660;
			toneBtn.y=260;
			addChild(toneBtn);
			
			var sinBtn:SinButton=new SinButton();
			sinBtn.x=730;
			sinBtn.y=270;
			sinBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{toneManager.toneFunction=ToneManager.Sin;grid.textDisplay.setLine(3,"TONE:SIN");});
			addChild(sinBtn);
			
			var squareBtn:SquareButton=new SquareButton();
			squareBtn.x=780;
			squareBtn.y=270;
			squareBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{toneManager.toneFunction=ToneManager.Square;grid.textDisplay.setLine(3,"TONE:SQUARE");});
			addChild(squareBtn);
			
			var triangleBtn:TriangleButton=new TriangleButton();
			triangleBtn.x=830;
			triangleBtn.y=270;
			triangleBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{toneManager.toneFunction=ToneManager.Triangle;grid.textDisplay.setLine(3,"TONE:TRIANGLE");});
			addChild(triangleBtn);
			
			var sawtoothBtn:SawtoothButton=new SawtoothButton();
			sawtoothBtn.x=880;
			sawtoothBtn.y=270;
			sawtoothBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{toneManager.toneFunction=ToneManager.Sawtooth;grid.textDisplay.setLine(3,"TONE:SAWTOOTH");});
			addChild(sawtoothBtn);
			
			var gateBtn:GateButton=new GateButton();
			gateBtn.x=660;
			gateBtn.y=340;
			gateBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{gateManager.takeControl(grid)});
			addChild(gateBtn);
			
			var smoothGateBtn:SmoothGateButton=new SmoothGateButton();
			smoothGateBtn.x=730;
			smoothGateBtn.y=350;
			smoothGateBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{gateManager.gateFunction=gateManager.SMOOTH;grid.manager.updateGrid();});
			addChild(smoothGateBtn);
			
			var squareGateBtn:SquareGateButton=new SquareGateButton();
			squareGateBtn.x=780;
			squareGateBtn.y=350;
			squareGateBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{gateManager.gateFunction=gateManager.SQUARE;grid.manager.updateGrid();});
			addChild(squareGateBtn);
			
			var delayGateBtn:DelayGateButton=new DelayGateButton();
			delayGateBtn.x=830;
			delayGateBtn.y=350;
			delayGateBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{gateManager.gateFunction=gateManager.DELAY;grid.manager.updateGrid();});
			addChild(delayGateBtn);
			
			var attackGateBtn:AttackGateButton=new AttackGateButton();
			attackGateBtn.x=880;
			attackGateBtn.y=350;
			attackGateBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{gateManager.gateFunction=gateManager.ATTACK;grid.manager.updateGrid();});
			addChild(attackGateBtn);
			
			var onGateBtn:OnGateButton=new OnGateButton();
			onGateBtn.x=930;
			onGateBtn.y=350;
			onGateBtn.addEventListener(MouseEvent.CLICK,function(event:MouseEvent):void{gateManager.gateFunction=gateManager.ON;grid.manager.updateGrid();});
			addChild(onGateBtn);
			
			var saveBtn:SaveButton=new SaveButton();
			saveBtn.x=660;
			saveBtn.y=420;
			addChild(saveBtn);
			saveBtn.addEventListener(MouseEvent.CLICK,exportLoop);
			
			sequencerManager.takeControl(grid);
		}
		
		private var octaveManager:OctaveManager;
		private var volumeManager:VolumeManager;
		private var sequencerManager:SequencerManager;
		private var toneManager:ToneManager;
		private var soundManager:SoundManager;
		private var gateManager:GateManager;
		
		private function exportLoop(event:MouseEvent):void{
			var fileRef:File=File.documentsDirectory;
			fileRef.browseForSave("Save Loop as WAVE File...");
			fileRef.addEventListener(Event.SELECT,saveLoopFile);
		}
		
		public function saveLoopFile(e:Event):void{
			soundManager.stop();
			var encoder:WAVEncoder=new WAVEncoder(false,WAVEncoder.SAMPLE_RATE_22050);
			encoder.writeData(soundManager.calculateSequence());
			encoder.makeFile(e.target as File);
		}
	}
}